# _*_coding:utf-8_*_
# coder:SKYNE->send email
import fileinput
import smtplib
from email.header import Header
from email.mime.text import MIMEText
from email.utils import formataddr, parseaddr


# 定义一个用于格式化邮件地址的函数
def _format_(addr):
    name, addr = parseaddr(addr)
    return formataddr((Header(name, 'utf-8').encode(), addr))


# 填写正确的邮件账号，密码，收件人，内容，标题，邮件服务器，端口地址

def get_account():
    qqlist = []
    with fileinput.input('qqlist1.xlsx') as lines:
        for line in lines:
            qqlist.append(line.strip('\n'))
    return qqlist


def sendmail(content):
    from_addr = "1163046964@qq.com"
    password = "jylmcdglmxbqijdb"
    to_addr = get_account()
    server_addr = "smtp.qq.com"
    message = MIMEText(content, 'html', 'utf-8')
    message['From'] = _format_('SKYNE<%s>' % from_addr)
    message['To'] = _format_('Friends<%s>' % to_addr)
    message['Subject'] = Header('SKYNE', 'utf-8').encode()
    # 建立邮件对象，发送信息
    mail_server = smtplib.SMTP(server_addr, 587)
    mail_server.starttls()
    mail_server.set_debuglevel(0)
    mail_server.login(from_addr, password)
    try:
        mail_server.sendmail(from_addr, to_addr, message.as_string())
    except smtplib.SMTPDataError:
        print("被反垃圾邮件驳回")
    except smtplib.SMTPServerDisconnected:
        print("未成功连接至邮件服务器")
    mail_server.quit()
    print("已成功发送邮件")

def main():
    content = ""
    sendmail(content)

if __name__ == '__main__':
    main()
